<?php
namespace app\controller\model;

use app\BaseController;
use think\facade\Db;
class CheckCar extends BaseController
{
    protected $carKey="f2dfdeb22b144840bac7d15cb1acae90";
    protected $mobileKey="f2dfdeb22b144840bac7d15cb1acae90";
	public $userID,$tables;
    public function __construct(){
		$this->tables=new Tables();
    }
	protected function img_base64($path){
		//对path进行判断，如果是本地文件就二进制读取并base64编码，如果是url,则返回
		$img_data="";
		if (substr($path,0,strlen("http")) === "http"){
			$img_data = $path;
		}else{
			if($fp = fopen($path, "rb", 0)) {
				$binary = fread($fp, filesize($path)); // 文件读取
				fclose($fp);
				$img_data = base64_encode($binary); // 转码
			}else{
				echojson([],1,'图片不存在');
			}
		}
		return $img_data;
	}
	public function checkMobile($mobile,$name,$idcard){
		$host = "https://kzmobilev2.market.alicloudapi.com";
		$path = "/api/mobile_three/check";
		$method = "GET";
		$appcode = $this->mobileKey;
		$headers = array();
		$name = urlencode($name);
		if($mobile=='' || $name=='' || $idcard=='') echojson([],1,'字段缺失');
		array_push($headers, "Authorization:APPCODE " . $appcode);
		$querys = "mobile=$mobile&name=$name&idcard=$idcard";
		$bodys = "";
		$url = $host . $path . "?" . $querys;
		$curl = curl_init();
		curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
		curl_setopt($curl, CURLOPT_URL, $url);
		curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
		curl_setopt($curl, CURLOPT_FAILONERROR, false);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($curl, CURLOPT_HEADER, true);
		if (1 == strpos("$".$host, "https://"))
		{
			curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
			curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
		}
		$result = curl_exec($curl);
		$header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
		$rheader = substr($result, 0, $header_size);
		$rbody = substr($result, $header_size);
		$re = json_decode($rbody,true) ;
		if($this->userID>0){
			if($re['success']==true && $re['data']['result']==0){
				DB::name($this->tables->users)->where("id",$this->userID)->update(['status'=>2]);
			}else{
				echojson($re,1,'手机实名失败');
			}
		}
		echojson($re,0,'已通过审核');
	}
	public function rCarLater($img_path){
		$url = "https://cardnumber.market.alicloudapi.com/rest/160601/ocr/ocr_idcard.json";
		$appcode = $this->carKey;
		$method = "POST";
		$headers = array();
		array_push($headers, "Authorization:APPCODE " . $appcode);
		//根据API的要求，定义相对应的Content-Type
		array_push($headers, "Content-Type".":"."application/json; charset=UTF-8");
		//如果没有configure字段，config设为空
		$config = array(
			"side"=>"face"
		);
		$img_data=$this->img_base64($img_path);
		$request = array(
			"image" => "$img_data"
		);
		if(count($config) > 0){
			$request["configure"] = json_encode($config);
		}
		$body = json_encode($request);
		$curl = curl_init();
		curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
		curl_setopt($curl, CURLOPT_URL, $url);
		curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
		curl_setopt($curl, CURLOPT_FAILONERROR, false);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($curl, CURLOPT_HEADER, true);
		if (1 == strpos("$".$url, "https://")){
			curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
			curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
		}
		curl_setopt($curl, CURLOPT_POSTFIELDS, $body);
		$result = curl_exec($curl);
		$header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
		$rheader = substr($result, 0, $header_size);
		$rbody = substr($result, $header_size);
		$httpCode = curl_getinfo($curl,CURLINFO_HTTP_CODE);
		if($httpCode == 200){
			$result_str = $rbody;
			if($this->userID>0){
				DB::name($this->tables->users)->where("id",$this->userID)->update(['carImgCheck'=>1]);
			}
			echojson(json_decode($result_str,true),0,$img_path);
		}else{
			if($this->userID>0){
				DB::name($this->tables->users)->where("id",$this->userID)->update(['carImgCheck'=>0]);
			}
//			printf("Http error code: %d\n", $httpCode);
//			printf("Error msg in body: %s\n", $rbody);
//			printf("header: %s\n", $rheader);
			$result_str = $rbody;
			echojson([],1,'识别失败');
		}
	}
}
?>